<?php
    // +----------------------------------------------------------------------
    // | 用户私信聊天记录
    // +----------------------------------------------------------------------
    // | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
    // +----------------------------------------------------------------------
    // | Author: 微尘 <yicmf@qq.com>
    // +----------------------------------------------------------------------
    namespace app\model\pos;

    use app\model\pay\Pay;
    use app\model\ucenter\User;
    use think\model\concern\SoftDelete;
    use yicmf\tools\FlowSn;

    class PosOrder extends Common
    {
        use SoftDelete;


        public function posVip()
        {

            return $this->belongsTo(PosVip::class,'pos_vip_id');
        }

        public function posVips()
        {

            return $this->hasMany(PosOrderVip::class);
        }

        public function poses()
        {

            return $this->hasMany(Pos::class,'order_id','id');
        }


        public function items()
        {

            return $this->hasMany(PosOrderItem::class);
        }


        public function getAddressInfoTextAttr($value,$data)
        {
            if ($data['send_type'] == 1)
            {
                $info = json_decode($data['address_info'], true);
                return '姓名：'.$info['name'].' 手机号：'.$info['mobile'].' 地址：'.$info['province_name'].$info['city_name'].$info['country_name'].$info['address'].$info['detail_info'];
            }else{
                return  '';
            }
        }
        public function user()
        {
            return $this->belongsTo(User::class);
        }
        public function pay()
        {
            return $this->belongsTo(Pay::class,'sn','pay_sn');
        }
        /**
         * 写入修改
         * @param $data
         */
        public static function onBeforeInsert($data)
        {
            $data['sn'] = FlowSn::build(1);
            $data['ip'] = !empty($data['ip']) ? $data['ip'] : self::$request->ip();
            $data['status'] = 1;
        }
        protected $type = [
            'address_info'=>'array'
        ];
    }
